<template>
	<view>
		<view class="shopping" v-for="item in cart" :key="item.id">
			<view>
				<image :src="item.image"></image>
			</view>
			<view class="center">
				<view class="top">
					<view class="top-title">{{item.name}}</view>
					<view class="zhe">折</view>
				</view>
				<view class="notes">1人份 | 微辣 | 不要孜然</view>
				<view class="footer">
					<view class="number" @click="show = item.id" v-if="show !== item.id">x{{item.cart_num}}</view>
					<view class="numberbox" v-else>
						<u-number-box v-model="item.cart_num"
						    bgColor="#fafafa"
							button-size="16"
							@change="valChange(item.id,item.cart_num,$event)"></u-number-box>
						<view class="iconfont icon-quxiao" @click="off"></view>
					</view>
					<view class="price">
						<text style="text-decoration:line-through;margin-top: 2rpx;" v-if="false">￥69.90</text>
						<text style="color: #cc0022;margin-left: 16rpx;">￥</text>
						<text class="jiage">{{item.sales_price}}</text>
						<checkbox :value="item.id" class="custom-checkbox"
						 :checked="isChecked(item.id)" @click="delchenge(item.id,item.cart_num,item.sales_price)"
						  color="#025E21" style="transform:scale(0.7)"/>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import http from '@/utils/http.js'
	export default {
		props: {
			cart: {
				type: Array,
				default: () => []
			},
			del: {
				type: String,
				default: ''
			},
			all: {
				type: Boolean,
				default: false
			},
			newseletids:{
				type:Array,
				default:() => []
			}
		},
		data() {
			return {
				show: null,
				selectedItems: [], // 存储选中项的数组,
				summoney: 0
			}
		},
		watch: {
			all(newVlaue, oldVlaue) {
				if (newVlaue === true) {
					this.selectedItems = this.cart.map(item => item.id);
					this.$emit('selecteId', this.selectedItems)
				} else {
					this.selectedItems = []
				}
			},
			newseletids(value,_){
				if(value.length <= 0){
					this.selectedItems = [],
					this.summoney = 0
				}
			}
		},
		methods: {
			async valChange(id, number, el) {
				if (number > el.value) {
					// 减数量
					await http({
						url: `/cart/minus/${id}`,
						method:'PUT'
					})
				}
				if (number < el.value) {
					// 加数量
					await http({
						url: `/cart/plus/${id}`,
						method:'PUT'
					})
				}
			},
			off() {
				this.show = null,
					this.$emit('spcartapi', '')
			},
			isChecked(id) {
			  return this.selectedItems.includes(id);
			},
			delchenge(id, money, num) {
			  const index = this.selectedItems.indexOf(id);
			  if (index > -1) {
				this.selectedItems.splice(index, 1); // 取消勾选
			  } else {
				this.selectedItems.push(id); // 勾选
			  }
				this.$emit('selecteId', this.selectedItems)
				if (this.del !== 'del') {
					if (index <= -1) {
						this.summoney += money * Number(num)
					} else {
						this.summoney -= money * Number(num)
					}
					this.$emit('money', this.summoney)
				}
			}
		}
	}
</script>

<style lang="less">
	.shopping {
		display: flex;
		margin-top: 20rpx;

		image {
			width: 137rpx;
			height: 137rpx;
		}

		.center {
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			margin-left: 30rpx;
			width: 100%;

			.top {
				font-size: 28rpx;
				display: flex;
				align-items: center;

				.top-title {
					width: 228rpx;
					white-space: nowrap;
					/* 禁止文本换行 */
					overflow: hidden;
					/* 隐藏元素中的文本溢出部分 */
					text-overflow: ellipsis;
					/* 在文本溢出时显示省略号 */
				}

				.zhe {
					width: 30rpx;
					height: 30rpx;
					border-radius: 50%;
					background-color: #025E21;
					font-size: 14rpx;
					color: #fff;
					text-align: center;
					line-height: 30rpx;
					margin-left: 10rpx;
				}
			}

			.notes {
				font-family: PingFang SC;
				font-size: 20rpx;
				font-weight: 400;
				line-height: 28rpx;
				letter-spacing: 0em;
				text-align: left;
				color: #999999;
			}

			.footer {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.number {
					font-size: 20rpx;
					color: #000;
					border: 1px solid #c3c3c3;
					padding: 2rpx 6rpx;
					border-radius: 10rpx;
				}

				.numberbox {
					display: flex;
					align-items: center;

					.icon-quxiao {
						font-size: 20rpx;
						background-color: #ebecee;
						border-radius: 50%;
						padding: 10rpx;
					}
				}

				.price {
					margin-bottom: 4rpx;
					display: flex;
					align-items: center;

					text {
						color: #999999;
						font-size: 24rpx;
					}

					.jiage {
						font-size: 30rpx;
						color: #CC0022;
						margin-right: 28rpx;
					}
				}
			}
		}
	}
</style>